package com.jdyfy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jdyfy.dto.RoleUserCountDTO;
import com.jdyfy.dto.UserLoginDTO;
import com.jdyfy.entity.User;
import com.jdyfy.mapper.UserMapper;
import com.jdyfy.result.Result;
import com.jdyfy.service.UserService;
import com.jdyfy.utils.Md5Util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 用户控制层
 */
@RestController
@RequestMapping("/jdyfy")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private Md5Util md5Util;

    /**
     * 登录
     * @param userLoginDTO
     * @return
     */
    @PostMapping("/login")
    public Result<User> login(@RequestBody UserLoginDTO userLoginDTO) { //前端登录界面提交的数据
        log.info("用户登录：{}", userLoginDTO);
        // 验证用户登录信息
        User user = userService.login(userLoginDTO);
        return Result.success(user);
    }

    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    public Result<String> logout() {
        return Result.success();
    }

    //新增和修改
    @PostMapping("/user/save")
    public Result<User> save(@RequestBody User user){
        user.setPassword(Md5Util.getMd5String(user.getPassword()));
        userService.savee(user);
        User user_1 = userService.getByUsername(user.getUsername());
        return Result.success(user_1);
    }

    //修改密码
    @PostMapping("/user/savePassword")
    public Result<Boolean> savePassword(@RequestBody User user){
        user.setPassword(Md5Util.getMd5String(user.getPassword()));
        boolean b = userService.saveOrUpdate(user);
        return Result.success(b);
    }

    //查询所有数据
    @GetMapping
    public Result<List<User>> index(){
        List<User> all = userService.findAll();
        return Result.success(all);

    }
    //删除数据
    @DeleteMapping("/user/delete")
    public int deleteById(@RequestBody Long id){
        return userMapper.deleteById(id);
    }

    //根据id查询员工
    @GetMapping("/user/find/{id}")
    public Result<User> getById(@PathVariable Long id){
        User user = userMapper.selectById(id);
        return Result.success(user);
    }

    //根据用户名查询员工
    @GetMapping("/user/findByUsername")
    public Result<User> getByUsername(@RequestParam String username){
        User user = userService.getByUsername(username);
        return Result.success(user);
    }

    //根据姓名查询员工
    @GetMapping("/user/findByName")
    public Result<User> getByName(@RequestParam String name){
        User user = userService.getByName(name);
        return Result.success(user);
    }

    @GetMapping("/user/findByRoleId")
    public Result<List<User>> getByRoleId(@RequestParam Long roleId){
        List<User> users= userService.getUsersByRoleId(roleId);
        return Result.success(users);
    }

    @GetMapping("/userPage")
    public Result<IPage<User>> getUserPage(@RequestParam Integer pageNum,
                                           @RequestParam Integer pageSize,
                                           @RequestParam Long roleId,
                                           @RequestParam String department) {
        //创建分页对象
        IPage<User> page = new Page<>(pageNum, pageSize);

        //创建查询对象
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();

        //添加条件查询
        if (roleId == -1) {
            queryWrapper.in("role_id", Arrays.asList(2, 5));
        } else if(roleId == 0){

        } else {
            queryWrapper.eq("role_id", roleId);
        }

        if (department != null && !department.isEmpty()){
            queryWrapper.like("department", department);
        }

        //执行查询
        IPage<User> IPage = userService.page(page, queryWrapper);
        return Result.success(IPage);
    }

    // 统计角色对应的用户数量
    @GetMapping("/user/userCountByRole")
    public Result<List<RoleUserCountDTO>> getRoleUserCount() {
        return Result.success(userService.getUserCountByRole());
    }

}
